package com.myc.train.member.service;



import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.myc.train.common.exception.BusinessException;
import com.myc.train.common.exception.BusinessExceptionEnum;
import com.myc.train.common.utils.JwtUtil;
import com.myc.train.common.utils.SnowUtil;
import com.myc.train.member.domain.Member;
import com.myc.train.member.domain.MemberExample;
import com.myc.train.member.mapper.MemberMapper;
import com.myc.train.member.req.MemberLoginReq;
import com.myc.train.member.req.MemberRequestReq;
import com.myc.train.member.req.MemberSendCodeReq;
import com.myc.train.member.resp.MemberLoginResp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MemberService {

    private static final Logger LOG = LoggerFactory.getLogger(MemberService.class);

    @Autowired
    private MemberMapper memberMapper;

    public int count(){
        return Math.toIntExact(memberMapper.countByExample(null));
    }

    public Long register(MemberRequestReq req){

        String mobile = req.getMobile();
        //new一个实体条件类
        Member members = selectMember(mobile);

        //非空判断
        if(ObjectUtil.isNotNull(members)){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_EXIST);
        }

        //new一个实体类
        Member member = new Member();
        //实体类传值
        member.setId(SnowUtil.getSnowflakeNextId());
        member.setMobile(mobile);
        //添加到数据库
        memberMapper.insert(member);

        return member.getId();

    }

    public void sendCode(MemberSendCodeReq req){
        String mobile= req.getMobile();

        //定义查询条件，查出数据
        Member members = selectMember(mobile);

        //判断是否有值
        if(ObjectUtil.isNull(members)){
            //如果是空值就将其添加到数据库
            Member member = new Member();
            member.setId(SnowUtil.getSnowflakeNextId());
            member.setMobile(mobile);
            memberMapper.insert(member);
            LOG.info("注册成功！！");
        }else{
            LOG.info("等待验证码");
        }

//        String code = RandomUtil.randomString(4);

        String code ="8888";

        LOG.info("验证码为：{}",code);
    }

    public MemberLoginResp login(MemberLoginReq req){
        String mobile= req.getMobile();
        String code = req.getCode();

        //定义查询条件，查出数据
        Member members = selectMember(mobile);

        //判断是否有值
        if(ObjectUtil.isNull(members)){
            //如果是空值就说明手机号不对
           throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_NOT_EXIST);
        }

        if(!"8888".equals(code)){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_CODE_ERROR);
        }

        MemberLoginResp resp = new MemberLoginResp();
        resp.setId(SnowUtil.getSnowflakeNextId());
        resp.setMobile(mobile);
        String token = JwtUtil.createToken(resp.getId(), resp.getMobile());
        resp.setToken(token);
        return resp;
        //        return BeanUtil.copyProperties(members , MemberLoginResp.class);

    }

    private Member selectMember(String mobile) {
        MemberExample memberExample = new MemberExample();
        memberExample.createCriteria().andMobileEqualTo(mobile);
        List<Member> members = memberMapper.selectByExample(memberExample);

        if(CollUtil.isEmpty(members)){
            return null;
        }else{
            return members.get(0);
        }

    }


}
